/*
 * @Author: 郑宏杰
 * @Date: 2020-07-08 14:04:39
 * @LastEditTime: 2020-07-09 23:20:11
 * @LastEditors: Please set LastEditors
 * @Description: 登录模板
 * @FilePath: \考试系统\jingkaisystem\src\store\loginModule.js
 */
// import storage from "@/utils/storage.js"
import stroage from "../utils/storage"


let loginModule = {
    namespaced: true,
    state: {
        examToken: stroage.get("examToken") || "",  //密钥
        userInfo: stroage.get("userInfo") || ""    //用户信息
    },
    mutations: { //同步
        //上传token
        ["SET_TOKEN"](state, examToken) {
            state.examToken = examToken
            stroage.set("examToken", state.examToken)
        },
        //删除token
        ["REMOVE_TOKEN"](state) {
            state.examToken = ""
            stroage.remove(state.examToken)
        },
        //刷新token
        ["REFRESH_TOKEN"](state, newToken) {
            state.examToken = newToken
            stroage.set("examToken", state.examToken)
        },
        //上传用户信息
        ["USER_INFO"](state, userInfo) {
            state.userInfo = userInfo
            stroage.set("userInfo", state.userInfo)
        },
        //删除用户信息
        ["REMOVE_USERINFO"](state) {
            state.userInfo = ""
            stroage.remove(state.userInfo)
        },
    },
    actions: { //异步
        //异步获取token
        async setToken(context, examToken) {
            return await context.commit("SET_TOKEN", examToken)
        },
        //异步获取用户信息
        async setUserInfo(context, userInfo) {
            return await context.commit("USER_INFO", userInfo)
        }
    }

}


export default loginModule